package io.renren.modules.backstage.MQ;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.modules.backstage.dao.BorrowerDao;
import io.renren.modules.backstage.dao.ProductDao;
import io.renren.modules.backstage.dao.RepaymentDao;
import io.renren.modules.backstage.dao.UsersDao;
import io.renren.modules.backstage.entity.BorrowerEntity;
import io.renren.modules.backstage.entity.ProductEntity;
import io.renren.modules.backstage.entity.RepaymentEntity;
import io.renren.modules.backstage.entity.UsersEntity;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Component
//@RocketMQMessageListener(consumerGroup = "LAPTOP-T7NMFP8C", topic = "FinancialProject")
// implements RocketMQListener<String>
public class RocketMQReceiver {

//    @Resource
//    private RepaymentDao repaymentDao;
//
//    @Resource
//    private BorrowerDao borrowerDao;
//
//    @Resource
//    private ProductDao productDao;
//
//    @Resource
//    private UsersDao usersDao;
//
//    @Override
//    public void onMessage(String s) {
//        System.err.println(s);
//        List<RepaymentEntity> repaymentList = repaymentDao.selectList(null);
//        String currentDate = DateUtil.format(new Date(), "yyyy-MM-dd");
//        RepaymentEntity payment = null;
//        boolean flag = false;
//        for (RepaymentEntity repaymentEntity : repaymentList) {
//            String repaymentDate = DateUtil.format(new Date(repaymentEntity.getRepayTime().toString()), "yyyy-MM-dd");
//            if(currentDate.equals(repaymentDate)){
//                payment = repaymentEntity;
//                flag = true;
//            }
//        }
//        if(flag){
//            QueryWrapper<BorrowerEntity> borrowerWrapper = new QueryWrapper<>();
//            borrowerWrapper.eq("borrow_id",payment.getBorrowId());
//            BorrowerEntity borrowerEntity = borrowerDao.selectOne(borrowerWrapper);
//            QueryWrapper<UsersEntity> userWrapper = new QueryWrapper<>();
//            userWrapper.eq("user_id",payment.getUserId());
//            UsersEntity usersEntity = usersDao.selectOne(userWrapper);
//            QueryWrapper<ProductEntity> productWrapper = new QueryWrapper<>();
//            productWrapper.eq("product_id",borrowerEntity.getProductId());
//            ProductEntity productEntity = productDao.selectOne(productWrapper);
//            String message = "尊敬的用户,您在广大金融申请的"+productEntity.getCompanyName()+"现金贷款将于"+currentDate+"扣款," +
//                    "本期应还"+payment.getSumMoney()+"元,请确保卡内余额充足,以免影响扣款。如已还款,请忽略此短信 回复T退订";
//            System.err.println(message);
//            //SendMsgUtils.sendMsg(usersEntity.getPhone(),message);
//        }
//    }
}
